/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lbim.server.main;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 *
 * @author Jestan
 */
public class HttpRequestMessage {
    /** Map<String, String[]> */
    private Map headers = null;

    public void setHeaders(Map headers) {
        this.headers = headers;
    }

    public Map getHeaders() {
        return headers;
    }

    public String getContext() {
        String[] context = (String[]) headers.get("Context");
        return context == null ? "" : context[0];
    }

    public String[] getHeader(String name) {
        return (String[]) headers.get(name);
    }

    public String getRequestProperty(String name) {
        String[] param = getHeader(name);
        if (param != null) {
            StringBuilder sb = new StringBuilder();
            for (String s : param) {
                sb.append(s);
            }
            return sb.toString();
        } else {
            return null;
        }
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();

        Iterator it = headers.entrySet().iterator();
        while (it.hasNext()) {
            Entry e = (Entry) it.next();
            str.append(e.getKey() + " : "
                    + arrayToString((String[]) e.getValue(), ',') + "\n");
        }
        return str.toString();
    }

    public static String arrayToString(String[] s, char sep) {
        if (s == null || s.length == 0)
            return "";
        StringBuffer buf = new StringBuffer();
        if (s != null) {
            for (int i = 0; i < s.length; i++) {
                if (i > 0)
                    buf.append(sep);
                buf.append(s[i]);
            }
        }
        return buf.toString();
    }
}

